﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.DataProxy.Cache;
using Enter.Common;

namespace Enter.Services.DataCache
{
    public sealed class Cache : ICache
    {
        public T Get<T>(string key)
        {
            return CacheService.Current.Get<T>(key);
        }

        /// <summary>
        /// 是否使用缓存
        /// </summary>
        public bool IsCache()
        {
            return CacheService.Current.IsCache();
        }


        public void Remove(string key)
        {
            CacheService.Current.Remove(key);
        }

        public void Put(string key, object value)
        {
            Put(key, value, 0);
        }

        public void Put(string key, object value, int Seconds)
        {
            CacheService.Current.Put(key, value, Seconds);
        }

        public T Get<T>(string key,CacheHelper.CacheDelegate Cachedelegate)
        {
            return Get<T>(key, 0, Cachedelegate);
        }

        public T Get<T>(string key, int Seconds, CacheHelper.CacheDelegate Cachedelegate)
        {
            T t = CacheService.Current.Get<T>(key);
            if (t == null)
            {
                if (Cachedelegate != null)
                {
                    t = (T)Cachedelegate();
                }
                if (t != null)
                {
                    Put(key, t, Seconds);
                }
            }
            return t;
        }

        public void ReleaseConnection()
        {
            CacheService.Current.ReleaseConnection();
        }

        public void DelteAll()
        {
            CacheService.Current.DeltelCache();
        }
    }
}
